package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.util.HashUtil;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserFollowsService userFollowsService;
    // 用户登录
    public User login(String account, String password) {
        // 根据账号查询用户
        User user = userMapper.findByAccount(account);
        if (user != null && HashUtil.verifyPassword(password, user.getPassword())) {
            return user; // 登录成功
        }
        return null; // 登录失败
    }

    // 用户注册
    public boolean register(User user) {
        // 对密码进行哈希加密
        String hashedPassword = HashUtil.hashPassword(user.getPassword());
        user.setPassword(hashedPassword);
        // 插入用户信息到数据库
        int result = userMapper.insertUser(user);
        return result > 0; // 返回插入结果
    }

    // 根据用户 ID 查询用户信息
    public User findById(Long userId) {
        return userMapper.findById(userId);
    }
    // 根据用户名模糊查询用户
    public List<User> findByUsername(String username) {
        return userMapper.findByUsername(username);
    }
    // 更新密码
    public boolean updatePassword(Long id, String newPassword) {
        // 对新密码进行哈希
        String hashnewPassword = HashUtil.hashPassword(newPassword);
        // 更新数据库中的密码
        int result = userMapper.updatePassword(id, hashnewPassword);
        return result > 0;
    }

    // 更新用户资料
    public boolean updateUserInfo(Long userId, User user) {
        int result = userMapper.updateUserInfo(userId, user);
        return result > 0;
    }

    // 更新用户头像
    public boolean updateUserAvatar(Long userId, String avatar) {
        int result = userMapper.updateUserAvatar(userId, avatar);
        return result > 0;
    }

    // 获取当前用户信息
    public User getCurrentUser(HttpServletRequest request) {
        // 获取当前用户的 Cookie
        String userToken = getCookieValue(request, "userToken");
        if (userToken == null) {
            return null; // 用户未登录
        }

        // 根据 userToken（用户 ID）查询用户信息
        Long userId = Long.parseLong(userToken);
        return userMapper.findById(userId);
    }

    // 获取指定 Cookie 的值
    private String getCookieValue(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookieName)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
    // 获取当前用户的粉丝数量
    public int getFollowerCount(Long userId) {
        return userFollowsService.getFollowerCount(userId.intValue());
    }
    // 根据帖子 ID 获取帖子作者
    public User getPostAuthor(Long postId) {
        return userMapper.findPostAuthorByPostId(postId);
    }
}
